-- Copyright (c) 2024 Huawei Technologies Co., Ltd.
-- openUBMC is licensed under Mulan PSL v2.
-- You can use this software according to the terms and conditions of the Mulan PSL v2.
-- You may obtain a copy of Mulan PSL v2 at:
--         http://license.coscl.org.cn/MulanPSL2
-- THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
-- EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
-- MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
-- See the Mulan PSL v2 for more details.

local log = require "log"
local cjson = require "cjson"

local smc_instance = {
    buffer = {},
    filename = "",

    -- 读写命令共有字段
    ADDR_IDX = 1,
    COMMAND_CODE_IDX = 2, -- 下标为2
    DATA_IDX = 4, -- 下标为4

    -- 写命令独有字段
    WRITE_ADDR_IDX = 1,
    WRITE_COMMAND_CODE_IDX = 2, -- 下标为2
    WRITE_LENGTH_IDX = 3, -- 下标为3
    WRITE_OPCODE_IDX = 4, -- 下标为4
    WRITE_OPCODE_WIDTH = 4, -- 下标为4
    WRITE_DATA_IDX = 8, -- 下标为8

    -- 命令字
    SMC_WRITE_OPCODE = 0x20,
    SMC_READ_OPCODE = 0x21,
    SMC_WRITE_DATA = 0x22,

    -- OPCODE标志位
    RW_MASK = 0x100,
    MS_MASK = 0x200,
    PARA_MASK = 0xff,
    OPCODE_MASK = 0xfffffc00,
    RW_OFFSET = 8, -- 偏移为8
    MS_OFFSET = 9, -- 偏移为9
}

local op_info_instance = {
    rw_flag = nil,
    ms_flag = nil,
    para = nil,
    opcode = nil,
}

function op_info_instance:new(obj, rw_flag, ms_flag, para, opcode)
    obj = obj or {}
    setmetatable(obj, self)
    self.__index = self
    self.rw_flag = rw_flag
    self.ms_flag = ms_flag
    self.para = para
    self.opcode = opcode
    return obj
end

local baseWriteInfo_instance = {
    op_info = nil,
    length = nil,
    crc = nil,
    verify_data = nil,
}

function baseWriteInfo_instance:new(obj, op_info, length, crc, verify_data)
    obj = obj or {}
    setmetatable(obj, self)
    self.__index = self
    self.op_info = op_info or op_info_instance:new(nil, nil, nil, nil, nil)
    self.length = length
    self.crc = crc
    self.verify_data = verify_data
    return obj
end

local function copy_data(data, idx, length)
    if idx + length > #data + 1 then
        return {}
    end
    local result = {}
    for i = idx, idx + length - 1 do
        table.insert(result, data[i])
    end
    return result
end

function smc_instance:new(obj, buffer)
    obj = obj or {}
    setmetatable(obj, self)
    self.__index = self
    self.buffer = buffer or {}
    return obj
end

function smc_instance:parse_addr(addr_idx)
    return self.buffer[addr_idx]
end

function smc_instance:parse_command_code(command_code_idx)
    return self.buffer[command_code_idx]
end

function smc_instance:parse_length(length_idx)
    return self.buffer[length_idx]
end

function smc_instance:parse_opcode(opcode_idx, opcode_width)
    local opcode = 0
    for i = opcode_idx, opcode_idx + opcode_width - 1 do
        opcode = opcode * (2 ^ 8) -- 容量为2 ^ 8
        opcode = opcode + self.buffer[i]
    end

    local rw_flag = (opcode & self.RW_MASK) >> self.RW_OFFSET
    local ms_flag = (opcode & self.MS_MASK) >> self.MS_OFFSET
    local para = opcode & self.PARA_MASK
    local opcode = opcode & self.OPCODE_MASK
    return op_info_instance:new(nil, rw_flag, ms_flag, para, opcode)
end

function smc_instance:parse_data(data_idx, length)
    local result = {}
    for i = data_idx, data_idx + length - 1 do
        table.insert(result, self.buffer[i])
    end
    return result
end

function smc_instance:parse_crc()
    return self.buffer[#self.buffer]
end

function smc_instance:parse_base_write_command()
    if #self.buffer < 8 then -- 长度限制8
        log:print(LOG_ERROR, "parse_base_write_command, length error")
        return nil
    end
    local length = self:parse_length(self.WRITE_LENGTH_IDX)
    local op_info = self:parse_opcode(self.WRITE_OPCODE_IDX, self.WRITE_OPCODE_WIDTH)
    local crc = self:parse_crc()
    local verify_data = copy_data(self.buffer, 1, #self.buffer - 1)
    return baseWriteInfo_instance:new(nil, op_info, length, crc, verify_data)
end

function smc_instance:parse_write_opcode()
    return self:parse_base_write_command()
end

function smc_instance:parse_write_data()
    local base_write_info = self:parse_base_write_command()
    if base_write_info == nil then
        return
    end
    local write_data = self:parse_data(self.WRITE_DATA_IDX, base_write_info.length - self.WRITE_OPCODE_IDX)
    return base_write_info, write_data
end

function smc_instance:parse()
    local addr = self:parse_addr(self.ADDR_IDX)
    local command_code = self:parse_command_code(self.COMMAND_CODE_IDX)
    local base_write_info = baseWriteInfo_instance:new(nil, nil, nil, nil, nil)
    local write_data = nil
    if command_code == self.SMC_WRITE_OPCODE then
        base_write_info = self:parse_write_opcode()
    elseif command_code == self.SMC_WRITE_DATA then
        base_write_info, write_data = self:parse_write_data()
    end

    return {
        addr = addr,
        command_code = command_code,
        opcode = base_write_info.op_info.opcode,
        ms_flag = base_write_info.op_info.ms_flag,
        rw_flag = base_write_info.op_info.rw_flag,
        para = base_write_info.op_info.para,
        length = base_write_info.length,
        crc = base_write_info.crc,
        verify_data = base_write_info.verify_data,
        write_data = write_data,
    }
end

return smc_instance
